﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using System.Data;
using System.Web.Services;
using ProjectWebNhac.DTO;

namespace ProjectWebNhac.GUI.Admin
{
    public partial class QL_CaSi : System.Web.UI.Page
    {
        CaSiBus csbus = new CaSiBus();
        TheLoaiBus theloaiBus = new TheLoaiBus();
        QuocGiaBus quocgiaBus = new QuocGiaBus();
        BaiHatBus bhbus = new BaiHatBus();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                Loadpage();
                LoadGvDSCaSi();
                LoadDsQuocGia();
            }
        }
        private void CreateFriendlyGrid(GridView grView)
        {
            grView.UseAccessibleHeader = true;
            grView.HeaderRow.TableSection = TableRowSection.TableHeader;
            grView.FooterRow.TableSection = TableRowSection.TableFooter;
        }
        private void Loadpage()
        {
            imgHinhAnh.ImageUrl = "images/male-avatar-icon-614x460.png";
            txtCTyDaiDien.Text = "";
            txtNgaySinh.Text = "";
            TxtTenCaSi.Text = "";
            txtTheLoaiNhac.Text = "";
            txtTTChiTiet.Text = "";

            flhinhanh.Visible = false;
            txtCTyDaiDien.Enabled = false ;
            txtNgaySinh.Enabled = false;
            TxtTenCaSi.Enabled = false;
            txtTheLoaiNhac.Enabled = false;
            txtTTChiTiet.Enabled = false;

            lnkHuy.Visible = false;
            lnkThem.Visible = true;
            divEdit.Visible = false;
            lnkThem.Text = "Thêm";

        }

        private void LoadGvDSCaSi()
        {
            DataTable dt = csbus.LayDanhSachCaSi();

            gvDSCaSi.DataSource = dt;
            gvDSCaSi.DataBind();
            CreateFriendlyGrid(gvDSCaSi);
        }

        [WebMethod(EnableSession = false)]

        public static string HelloWorld()
        {
            return "Hello: " + DateTime.Now.Millisecond;
        }

        [WebMethod(EnableSession = false)]
        public static string SaveEdit(string TenCaSi, string MaCaSi, string NgaySinh)
        {
            
            return "Hello: " + DateTime.Now.Millisecond;
        }

        private void LoadDsQuocGia()
        {
            
            drQuocGia.DataSource = quocgiaBus.LayDanhSachQuocGia();
            drQuocGia.DataTextField = "TenQuocGia";
            drQuocGia.DataValueField = "MaQuocGia";
            drQuocGia.DataBind();
        }

        protected void gvDSCaSi_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("ChiTiet"))
            {

                int MaCS = Convert.ToInt16(e.CommandArgument);
                gvDSBaiHatCaSi.DataSource = bhbus.LoadDanhSachBaiHatTheoCaSi(MaCS);
                gvDSBaiHatCaSi.DataBind();
                if (gvDSBaiHatCaSi.Rows.Count>0)
                {
                    CreateFriendlyGrid(gvDSBaiHatCaSi);
                }
                LoadGvDSCaSi();
                DataTable dt = csbus.LayChiTietCasi(MaCS);
                TxtTenCaSi.Text = dt.Rows[0]["TenCaSi"].ToString();
                txtCTyDaiDien.Text = dt.Rows[0]["CongTyDaiDien"].ToString();
                drQuocGia.SelectedValue = dt.Rows[0]["MaQuocGia"].ToString();
                txtNgaySinh.Text = dt.Rows[0]["NgaySinh"].ToString();
                txtTTChiTiet.Text = dt.Rows[0]["ThongTinChiTiet"].ToString();
                imgHinhAnh.ImageUrl = "../../HinhAnh/CaSi/" + dt.Rows[0]["HinhAnh"].ToString();
                txtTheLoaiNhac.Text = theloaiBus.LayTheLoaiTheoCaSi(MaCS);
                txtMaCasi.Value = dt.Rows[0]["MaCaSi"].ToString();
                txtTenCaSiEdit.Text = dt.Rows[0]["TenCaSi"].ToString();
                txtCTyDaiDienEdit.Text = dt.Rows[0]["CongTyDaiDien"].ToString();
                drQuocGiaEdit.SelectedValue = dt.Rows[0]["MaQuocGia"].ToString();
                txtNgaySinhEdit.Text = dt.Rows[0]["NgaySinh"].ToString();
                txtTTChiTietEdit.Text = dt.Rows[0]["ThongTinChiTiet"].ToString();
                imgHinhAnhEdit.ImageUrl = "../../HinhAnh/CaSi/" + dt.Rows[0]["HinhAnh"].ToString();
                txtTheLoaiNhacEdit.Text = theloaiBus.LayTheLoaiTheoCaSi(MaCS);
                drQuocGiaEdit.DataSource = quocgiaBus.LayDanhSachQuocGia();
                drQuocGiaEdit.DataTextField = "TenQuocGia";
                drQuocGiaEdit.DataValueField = "MaQuocGia";
                drQuocGiaEdit.DataBind();
                divEdit.Visible = true;
                lnkThem.Visible = false;
                lnkHuy.Visible = true;
           }
        }

        protected void lnkThem_Click(object sender, EventArgs e)
        {
            if (lnkThem.Text == "Thêm")
            {
                txtCTyDaiDien.Enabled = true;
                txtNgaySinh.Enabled = true;
                TxtTenCaSi.Enabled = true;
                txtTheLoaiNhac.Enabled = true;
                txtTTChiTiet.Enabled = true;
                drQuocGia.Enabled = true;
                flhinhanh.Visible = true;
                lnkThem.Text = "Lưu";
                lnkHuy.Visible = true;
            }
            else if (lnkThem.Text.Equals("Lưu"))
            {
                CaSiDTO objCasi = new CaSiDTO();
                objCasi.TenCasi = TxtTenCaSi.Text;
                objCasi.NgaySinh = txtNgaySinh.Text;
                objCasi.ThongTinCaSi = txtTTChiTiet.Text;
                objCasi.MaQuocGia = Convert.ToInt16(drQuocGia.SelectedValue);
                objCasi.CongtyDaiDien = txtCTyDaiDien.Text;
                objCasi.Theloai = txtTheLoaiNhac.Text;
                if (flhinhanh.FileName.Length>0)
                {
                    objCasi.HinhAnh = flhinhanh.FileName;
                    flhinhanh.SaveAs(Server.MapPath("../../HinhAnh/CaSi/"+flhinhanh.PostedFile.FileName));
                } 
                else
                {
                    objCasi.HinhAnh = "male-avatar-icon-614x460.png";
                }
                if (csbus.ThemCasi(objCasi))
                {
                    pnSuccess.Visible = true;
                    LoadGvDSCaSi();
                    Loadpage();
                    lnkThem.Text = "Thêm";
                    lnkHuy.Visible = false;
                }
            }
            else
            {

                lnkThem.Text = "Thêm";
                lnkHuy.Visible = false;
            }
        }

        protected void lnkHuy_Click(object sender, EventArgs e)
        {
            Loadpage();
            LoadGvDSCaSi();
            LoadDsQuocGia();
        }

        protected void gvDSCaSi_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                Label lbTheLoai = (Label)e.Row.FindControl("lbTheLoai");
                Label lbMaCaSi = (Label)e.Row.FindControl("lbMaCaSi");
                int MaCS = Convert.ToInt16(lbMaCaSi.Text);
                lbTheLoai.Text = theloaiBus.LayTheLoaiTheoCaSi(MaCS);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            CaSiDTO objCs = new CaSiDTO();
            objCs.MaCasi = Convert.ToInt16(txtMaCasi.Value);
            objCs.TenCasi = txtTenCaSiEdit.Text;
            objCs.MaQuocGia = Convert.ToInt16(drQuocGiaEdit.SelectedValue);
            objCs.NgaySinh = txtNgaySinhEdit.Text;
            objCs.ThongTinCaSi = txtTTChiTietEdit.Text;
            objCs.Theloai = txtTheLoaiNhacEdit.Text;
            objCs.CongtyDaiDien = txtCTyDaiDienEdit.Text;
            if (csbus.CapNhatThongTin(objCs))
            {
                pnSuccess.Visible = true;
                LoadGvDSCaSi();
                Loadpage();
            }

        }

    }
}